﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using iBook2.Models;

namespace iBook2.Controllers
{
    public class CommentController : ApiController
    {
        private iBookEntities db = new iBookEntities();

        // GET api/Comment
        public IEnumerable<BinhLuan> GetBinhLuans()
        {
            var binhluans = db.BinhLuans.Include(b => b.Sach).Include(b => b.TaiKhoan);
            return binhluans.AsEnumerable();
        }

        // GET api/Comment/5
        public BinhLuan GetBinhLuan(string id)
        {
            BinhLuan binhluan = db.BinhLuans.Find(id);
            if (binhluan == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return binhluan;
        }

        // PUT api/Comment/5
        public HttpResponseMessage PutBinhLuan(string id, BinhLuan binhluan)
        {
            if (ModelState.IsValid && id == binhluan.MaBinhLuan)
            {
                db.Entry(binhluan).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Comment
        public HttpResponseMessage PostBinhLuan(BinhLuan binhluan)
        {
            if (ModelState.IsValid)
            {
                db.BinhLuans.Add(binhluan);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, binhluan);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = binhluan.MaBinhLuan }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Comment/5
        public HttpResponseMessage DeleteBinhLuan(string id)
        {
            BinhLuan binhluan = db.BinhLuans.Find(id);
            if (binhluan == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.BinhLuans.Remove(binhluan);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, binhluan);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}